<?php
/**
 * 工作日志填写
 * Enter description here ...
 * @author cc
 *
 */
namespace Home\Controller;
use Think\Controller;
class WorkLogController extends CommonController{
	public function way(){
		session("action","WorkLog");
		session("menu_id",I('get.menu_id'));
		$this->redirect("Public/index");
	}

	function daily(){//日报表
		$user_name = session("user_name");
		$daily = M("worklog_daily_set");
		$data_user = $daily->where("user_name = '$user_name'")->find();
		if(count($data_user) == 0){
			echo "恭喜你，你不用每日填写日报表！O(∩_∩)O哈哈~";exit;
		}else{
			$worklog = M("worklog_daily");
			$user_name = session('user_name');
			$count = $worklog->where("user_name='$user_name'")->count();
			$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
			$show = $page->show();
			$data = $worklog->where("user_name='$user_name'")->order('log_date desc')->limit($page->firstRow.','.$page->listRows)->select();
			
			$this->assign('data',$data);
			$this->assign("page",$show);
			$this->assign("empty",emptyhtml("list"));
			$this->display();
		}
	}
	
	function daily_add(){
		$time = date('Ymd');
		$model = M();
		$user_name = session('user_name');
		$data_worklog = M("worklog_daily")->where("to_days(log_date)=to_days(now()) and user_name='$user_name'")->find();
		if(empty($data_worklog)){
			$data = $model->query("SELECT d.dept_name FROM oa_sys_user u, oa_sys_dept d WHERE u.user_name='$user_name' and u.dept_id=d.id");
			$vo['log_name'] = $time.$data[0]['dept_name'].'日报表--'.session('name');
			$vo['status'] = 0;
			$this->assign('vo', $vo);
		}else{
			$this->assign('vo', $data_worklog);
		}
		$this->display();
	}
	
	function daily_insert(){
		if(!IS_POST) $this->error("非法表单");exit;
		$log_id = I('post.log_id');
		$data = array(
			'log_name' => $_POST['log_name'],
			'log_text' => $_POST['log_text'],
			'log_plan' => $_POST['log_plan'],
			'log_question' => $_POST['log_question'],
			'log_date' => date('Y-m-d'),
			'user_name' => session('user_name'),
			'dept_id' => session('dept_id'),
			'status' => $_POST['flag']
		);
		$data['log_stime'] = $_POST['flag'] == 1 ? date('Y-m-d H:i:s') : '';
		if(isset($log_id) && !empty($log_id)){
			$data['log_utime'] = date('Y-m-d H:i:s');
			$success = M("worklog_daily")->where('log_id='.$log_id)->save($data);
		}else{
			$data['log_ctime'] = date('Y-m-d H:i:s');
			$success = M("worklog_daily")->add($data);
		}
		if($success){
			$this->systemlog("添加", "日报表", session('name')."添加日报表".$_POST['log_name']);
			$this->success('添加工作日志成功','daily');
		}else{
			$this->error('添加工作日志失败');
		}
	}
	
	function daily_look(){
		$log_id = I("get.id");
		$user_name = session('user_name');
		$data_worklog = M("worklog_daily")->where("user_name='$user_name' and log_id=%d",$log_id)->find();
		$this->assign('vo', $data_worklog);
		$this->display();
	}
	
	function search(){//绩效查询
		$sql = "SELECT u.name, u.id as user_id, u.user_name, d.id as dept_id, d.dept_name FROM oa_sys_user u, oa_sys_dept d, oa_worklog_daily w WHERE ";
		$sql1 = "SELECT * FROM oa_sys_user u, oa_sys_dept d, oa_worklog_daily w WHERE ";
		if(!empty($_POST['username'])){
			$username = $_POST['username'];
			$sql .= "u.name like '%$username%' and ";
			$sql1 .= "u.name like '%$username%' and ";
		}
		if(!empty($_POST['dept_id'])){
			$dept_id = $_POST['dept_id'];
			$sql .= "d.id = $dept_id and ";
			$sql1 .= "d.id = $dept_id and ";
		}
		$month = $_POST['month'] ? $_POST['month'] : date('Y-m');
		$sql .= " u.dept_id = d.id and u.status = '在职' and u.user_name != 'admin' group by u.id";
		$sql1 .= " u.dept_id = d.id and u.status = '在职' and u.user_name != 'admin' group by u.id";
		
		$model = M();
		$data1 = $model->query($sql1);
		$count = count($data1);
		$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
		$show = $page->show();
		$sql .= " limit $page->firstRow , $page->listRows";
		
		$data = $model->query($sql);
		foreach ($data as $key=>$value){
			$user_name = $value['user_name'];
			$data[$key]['count'] = M("worklog_daily")->where("user_name='$user_name' and date_format(log_date,'%Y-%m')='$month' and status=1")->count();
			$data[$key]['log_date'] = M("worklog_daily")->where("user_name='$user_name' and date_format(log_date,'%Y-%m')='$month' and status=1")->order('log_stime desc')->getField('log_date');
		}
		
		$data_dept = M("oa_sys_dept")->select();
		
		$this->assign('data_dept', $data_dept);
		$this->assign('username', $username);
		$this->assign('dept_id', $dept_id);
		$this->assign('month', $month);
		$this->assign('data', $data);
		$this->assign("page",$show);
		$this->assign("empty",emptyhtml("list"));
		$this->display();
	}
	
	function personal_worklog(){
		$user_id = I("get.id");
		
		$model = M();
		$sql = "SELECT w.* FROM oa_sys_user u, oa_worklog_daily w WHERE u.id=$user_id AND u.user_name=w.user_name and w.status=1";
		$data = $model->query($sql);
		$this->assign('data', $data);
		$this->display();
	}
	
	public function daily_view(){
		$log_id = I("get.id");
		$this->is_id($log_id);
		$data_worklog = M("worklog_daily")->where("log_id=$log_id")->find();
		$this->assign('vo', $data_worklog);
		$this->display();
	}
	
	public function weekly(){//周报表
		$user_name = session('user_name');
		$weekly = M("worklog_weekly_set");
		$data_user = $weekly->where("user_name = '$user_name'")->find();
		if(count($data_user) == 0){
			echo "恭喜你，你不用每周五填写周报表！O(∩_∩)O哈哈~";exit;
		}else{
			$worklog = M("worklog_weekly");
			$count = $worklog->where("user_name='$user_name'")->count();
			$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
			$show = $page->show();
			$data = $worklog->where("user_name='$user_name'")->order('log_date desc')->limit($page->firstRow.','.$page->listRows)->select();
			
			$this->assign('data',$data);
			$this->assign("page",$show);
			$this->assign("empty",emptyhtml("list"));
			$this->display();
		}
	}
	
	public function weekly_add(){
		$time = date('Ymd');
		$model = M();
		$weekarray=array("一","二","三","四","五");
		$w = date("w");
		switch ($w){
			case 0 :
				$a = -6;break;
			case 1 :
				$a = 0;break;
			case 2 :
				$a = -1;break;
			case 3 :
				$a = -2;break;
			case 4 :
				$a = -3;break;
			case 5 :
				$a = -4;break;
			case 6 :
				$a = -5;break;
		}
		$d = date("d",strtotime("$a day"));
		$month = date("Ym",strtotime("$a day"));
		$week = ceil($d/7);
		$week = $weekarray[$week];
		
		
		$user_name = session('user_name');
		$data_worklog = M("worklog_weekly")->where("to_days(log_date)=to_days(now()) and user_name='$user_name'")->find();
		if(empty($data_worklog)){
			$data = $model->query("SELECT d.dept_name FROM oa_sys_user u, oa_sys_dept d WHERE u.user_name='$user_name' and u.dept_id=d.id");
			$vo['log_name'] = $month."第".$week."周".$data[0]['dept_name'].'周报表--'.session('name');
			$vo['status'] = 0;
			$this->assign('vo', $vo);
		}else{
			$this->assign('vo', $data_worklog);
		}
		$this->display();
	}

	function weekly_insert(){
		$log_id = I('post.log_id');
		
		$data = array(
			'log_name' => $_POST['log_name'],
			'log_text' => $_POST['log_text'],
			'log_plan' => $_POST['log_plan'],
			'log_question' => $_POST['log_question'],
			'log_date' => date('Y-m-d'),
			'user_name' => session('user_name'),
			'dept_id' => session('dept_id'),
			'status' => $_POST['flag']
		);
		$data['log_stime'] = $_POST['flag'] == 1 ? date('Y-m-d H:i:s') : '';
		if(isset($log_id) && !empty($log_id)){
			$data['log_utime'] = date('Y-m-d H:i:s');
			$success = M("worklog_weekly")->where('log_id='.$log_id)->save($data);
		}else{
			$data['log_ctime'] = date('Y-m-d H:i:s');
			$success = M("worklog_weekly")->add($data);
		}
		if($success){
			$this->systemlog("添加", "周报表", session('name')."添加周报表".$_POST['log_name']);
			$this->success('添加工作日志成功','weekly');
		}else{
			$this->error('添加工作日志失败');
		}
	}
	
	public function weekly_look(){
		$log_id = I('get.log_id');
		$user_name = session('user_name');
		$data_worklog = M("worklog_weekly")->where("user_name='$user_name' and log_id=%d",$log_id)->find();
		$this->assign('vo', $data_worklog);
		$this->display();
	}
	
	public function monthly(){//月报表
		$model = M("worklog_monthly_set");
		$user_name = session('user_name');
		$data_user = $model->where("user_name = '$user_name'")->find();
		if(count($data_user) == 0){
			echo "恭喜你，你不用每月月底填写下个月的月度目标！O(∩_∩)O哈哈~";exit;
		}else{
			load('extend');
			$monthly = M("Oa_worklog_monthly");
			$count = $monthly->where("user_name='$user_name'")->count();
			$page = new \Think\Page($count,20);
			$show = $page->show();
			$data = $monthly->where("user_name='$user_name'")->order('log_date desc')->limit($page->firstRow.','.$page->listRows)->select();
			
			$this->assign('data',$data);
			$this->assign("page",$show);
			$this->assign("empty",emptyhtml("list"));
			$this->display();
		}
	}
	
	//public function monthly_add(){
	//	$this->display();
	//}
	
	public function monthly_insert(){
		$time = date('Y-m-d H:i:s');
		$date = date('Y-m-d', strtotime("$time +1 month"));
		$dat = date('Ym', strtotime("$time +1 month"));
		$log_id = $_POST['log_id'];
		
		$log_text = $_POST["log_text"];
		$weight = $_POST["weight"];
		$over_time = $_POST["over_time"];
		
		$content = "";
		$monthly = M("worklog_monthly");
		for ($i=0; $i<count($log_text); $i++){
			$content .= $log_text[$i]."%%".$weight[$i]."%%".$over_time[$i]."%!%";
		}
		$content = substr($content, 0, -3);
		$log_name = $dat."月份月度目标计划表--".session('name');
		$data = array(
			'log_name' => $log_name,
			'log_content' => $content,
			'log_question' => $_POST['log_question'],
			'log_date' => $date,
			'log_utime' => $time,
			'user_name' => session('user_name'),
			'dept_id' => session('dept_id'),
			'status' => $_POST['flag']
		);
		if( $_POST['flag'] == "1" )
			$data['log_stime'] = $time;
		
		if(empty($log_id)){
			$data['log_ctime'] = $time;
			$success = $monthly->add($data);
		}else{
			$success = $monthly->where('log_id='.$log_id)->save($data);
		}
		if($success){
			$this->systemlog("add","月度目标", session('name')."添加月度目标".$log_name);
			$this->success('保存月度目标表成功','monthly');
		}else{
			$this->error('保存月度目标表失败');
		}
	}
	
	public function monthly_edit(){
		$monthly = M("worklog_monthly");
		$user_name = session('user_name');
		
		$time = date('Y-m-d H:i:s');
		$date = date('Y-m', strtotime("$time +1 month"));
		$data = $monthly->where("user_name = '$user_name' AND date_format(log_date,'%Y-%m')='".$date."'")->find();
		if(count($data) == 0){
			$count = 0;
		}else{
			//dump($data);
			$log_centent = explode("%!%", $data['log_content']);
			//dump($log_centent);
			foreach ($log_centent as $key=>$value){
				$log_list = explode("%%", $value);
				$vo[$key]['log_text'] = $log_list[0];
				$vo[$key]['weight'] = $log_list[1];
				$vo[$key]['over_time'] = $log_list[2];
			}
			$count = count($data);
		}
		
		$this->assign("data", $data);
		$this->assign("count", $count);
		$this->assign("list", $vo);
		$this->assign("empty",emptyhtml("list"));
		//dump($vo);exit;
		$this->display();
	}
	
	public function monthly_look(){
		$monthly = M("worklog_monthly");
		$user_name = session('user_name');
		$id = $_GET['_URL_'][3];
		$data = $monthly->where("user_name = '$user_name' AND log_id = $id")->find();
		if(count($data) == 0){
			$count = 0;
		}else{
			$log_centent = explode("%!%", $data['log_content']);
			foreach ($log_centent as $key=>$value){
				$log_list = explode("%%", $value);
				$vo[$key]['log_text'] = $log_list[0];
				$vo[$key]['weight'] = $log_list[1];
				$vo[$key]['over_time'] = $log_list[2];
			}
			$count = count($data);
		}
		
		$this->assign("data", $data);
		$this->assign("count", $count);
		$this->assign("list", $vo);
		$this->display();
	}
	
	public function debriefings(){//月度述职报告
		$debriefings = M("worklog_debriefings_set");
		$user_name_ = $debriefings->where("id=1")->getField("user_name");
		$user_arr = explode(",", $user_name_);
		$user_name = session('user_name');
		if( !in_array($user_name, $user_arr) ){
			echo "你无权限填写月度述职报告！O(∩_∩)O哈哈~";exit;
		}else{
			load('extend');
			$model = M("worklog_debriefings");
			$count = $model->where("user_name='$user_name'")->count();
			$page = new \Think\Page($count,20);
			$show = $page->show();
			$data = $model->where("user_name='$user_name'")->order('create_date desc')->limit($page->firstRow.','.$page->listRows)->select();
			
			$this->assign("page",$show);
			$this->assign("data",$data);
			$this->assign("empty",emptyhtml("list"));
			$this->display();
		}
	}
	
	public function debriefings_add(){
		$this->display();
	}
	
	public function debriefings_upload(){
		$targetFolder = '/Public/upload/debriefings/'.date('Ymd'); // Relative to the root
		$verifyToken = md5('unique_salt' . $_POST['timestamp']);
		if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
			$tempFile = $_FILES['Filedata']['tmp_name'];
			$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
			if(!is_dir($targetPath)){
				mkdir($targetPath);
			}
			$filename = $_FILES['Filedata']['name'];
			$fileParts = pathinfo($filename);
			$extension = $fileParts['extension'];
			
//			dump($_FILES);exit;
			// Validate the file type
			$fileTypes = array('xlsx','xls','csv'); // File extensions
			$targetFile = rtrim($targetPath,'/') . '/' . $filename;
			$targetFile = iconv("UTF-8", "GB2312", $targetFile);
//			exit;
			if (in_array($extension,$fileTypes)) {
				move_uploaded_file($tempFile,$targetFile);
				$model = M("worklog_debriefings");
				$data = array(
					'name' => $filename,
					'file_path' => $targetFolder.'/'.$filename,
					'user_name' => session('user_name'),
					'dept_id' => session('dept_id'),
					'create_date' => date("Y-m-d"),
					'status' => 0,
				);
				$success = $model->add($data);
				echo "月度述职报告文件上传成功！";
			}
		}
	}
	
	public function debriefings_del(){
		$id = $_REQUEST['id'];
		
		$user_name = session('user_name');
		$model = M("worklog_debriefings");
		$data = $model->where("user_name = '$user_name' and id=$id")->find();
		$file_name = $_SERVER['DOCUMENT_ROOT'] . $data['file_path'];
		$file_name = iconv("UTF-8", "GB2312", $file_name);
		if(file_exists($file_name)){
			unlink($file_name);
		}
		$success = $model->where("user_name = '$user_name' and id=$id")->delete();
		if($success){
			$this->systemlog("del","月度述职报告", session('name')."删除月度述职报告".$data['log_name']);
			$this->success("删除月度述职报告成功！","debriefings");
		}else{
			$this->success("删除月度述职报告失败！");
		}
	}
	
	public function debriefings_filing(){
		$id = $_REQUEST['id'];
		
		$user_name = session('user_name');
		$model = M("worklog_debriefings");
		$data = array(
			'send_date' => date("Y-m-d"),
			'status' => 1
		);
		$model->where("user_name = '$user_name' and id=$id")->save($data);
	}
    
    public function _empty($name){
    	$this->emptyaction($name);
    }
}
?>